<template>
	<view class="feedback">
		<u-form labelPosition="top" :model="formData" :rules="rules" labelWidth="240rpx" ref="form">
			<u-form-item label="意见反馈" prop="feedback" required>
				<u-textarea v-model="formData.feedback" placeholder="请输入" border="none" />
			</u-form-item>
			<u-form-item label="上传图片" required>
				<!-- <u-upload
					:fileList="fileList"
					@afterRead="afterRead"
					@delete="deletePic"
					name="file"
					multiple
					:maxCount="10"
					previewFullImage
				/> -->
				<u-upload :fileList="fileList1" name="1" width="70" height="70" @afterRead="afterRead"
					@delete="deletePic" multiple :maxCount="1" />
			</u-form-item>
			<u-form-item label="联系电话" prop="phone" required>
				<u-input v-model="formData.phone" placeholder="请输入" border="none" type="number" />
			</u-form-item>
		</u-form>

		<u-button text="提交" shape="circle" @click="submit"
			color="linear-gradient(0deg, rgb(47, 117, 250) 0%, rgb(82, 142, 255) 100%)"></u-button>
	</view>
</template>

<script>
	const train = require("@/api/train/index.js");
	export default {
		data() {
			return {
				formData: {
					feedback: ''
				},
				rules: {
					feedback: {
						required: true,
						message: '请输入意见反馈',
						trigger: ['change'],
					},
					phone: {
						required: true,
						message: '请输入联系电话',
						trigger: ['change'],
					},
				},
				fileList: [],
				fileList1: [],
				url_phone: '',
				resulturl: '',
			}
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			// this.$refs.form1.setRules(this.rules)
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				this.resulturl = url
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://sass-test.doit10019.com/api/upload/uploadOneImage', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							// user: 'test',
							project_id: getApp().globalData.projectId,
							app_id: this.$https.weixinAppId,
						},
						header: {
							'content-type': 'multipart/form-data'
						},
						success: (res) => {
							let result = JSON.parse(res.data);
							this.url_phone = result.data.url
							setTimeout(() => {
								resolve(this.url_phone)
							}, 1000)
						}
					});
				})
			},
			// deletePic(event) {
			// 	this.fileList.splice(event.index, 1);
			// },
			// async afterRead(event) {
			// 	console.log(event.file)
			// 	this.fileList.concat(event.file);
			// },

			async submit() {
				try {
					const valid = await this.$refs.form.validate();
					if (valid) {
						let postData = {
							// type: 0,
							app_id: this.$https.weixinAppId,
							project_id: getApp().globalData.projectId,
							open_id: uni.getStorageSync("openid"),
							content: this.formData.feedback,
							pic: this.url_phone,
							phone: this.formData.phone,
						}
						let res = await train.Suggestion(postData)
						if (res.code == 1) {
							uni.navigateTo({
								url: '/pages/personal/personal'
							})
						}
					}
				} catch (e) {

				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.feedback {
		min-height: 100vh;
		background-color: #f3f3f3;
		padding: 30rpx;

		/deep/.u-form {
			background-color: #fff;
			border-radius: 10rpx;
			margin-bottom: 20rpx;
			padding: 20rpx 0;

			.u-form-item {
				padding: 0 30rpx;
			}
		}
	}
</style>
